package cn.com.dhc.danlu.fragment.base;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.fragment.bridge.FragmentBridge;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.CountDownTimer;
import cn.com.dhc.danlu.util.ToastUtil;

/**
 * @author wuhaiyang
 */
public abstract class BaseActivePhoneFragment extends BaseBridgeFragment implements OnClickListener {
    protected Button btn_getcode;
    protected EditText et_phone;
    protected EditText et_code; // 短信验证码输入框
    protected Button btn_next;

    private CountDownTimer timer = new CountDownTimer(60000, 1000) {
        @SuppressLint("SetTextI18n")
        @Override
        public void onTick(long millisUntilFinished) {
            if (null != getActivity() && !getActivity().isFinishing()) {
                btn_getcode.setText(millisUntilFinished / 1000 + "秒后重新获取");
            } else {
                timer.cancel();
            }
        }

        @Override
        public void onFinish() {
            btn_getcode.setText(getResources().getString(R.string.register_verify_code));
            btn_getcode.setEnabled(true);
            setDisAllowInputPhone(true);
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return createView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        btn_getcode = (Button) getView().findViewById(R.id.btn_getcode);
        btn_next = (Button) getView().findViewById(R.id.btn_verify);
        et_phone = (EditText) getView().findViewById(R.id.tv_phone);
        et_code = (EditText) getView().findViewById(R.id.et_smscode);
        btn_getcode.setOnClickListener(this);
        btn_next.setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);

    @Override
    public void onClick(View v) {
        String phone = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            ToastUtil.getInstance().show("请输入手机号码");
            return;
        }
        if (v.getId() == R.id.btn_getcode) {
            if (!CommonUtils.isMobileNumber(phone)) {
                ToastUtil.getInstance().show(R.string.register_tel_msg);
                return;
            }
            mBridgeCallback.onHandle(FragmentBridge.TYPE_GET_CODE, phone);
        } else if (v.getId() == R.id.btn_verify) {
            String code = et_code.getText().toString().trim();
            if (TextUtils.isEmpty(code)) {
                ToastUtil.getInstance().show("请输入验证码");
                return;
            }
            mBridgeCallback.onHandle(FragmentBridge.TYPE_VERIFY_CODE, code);
        }
    }

    /**
     * 开始倒计时，并设置获取验证码按钮为不可点击
     */
    public void startTimer() {
        //  请求服务器获取短信验证码
        // 开启定时器60
        timer.cancel();
        timer.start();
        btn_getcode.setEnabled(false);
    }

    /**
     * 重新获取短信验证码
     */
    public void reGetSmsCode() {
        btn_getcode.setEnabled(true);
        btn_getcode.setText(getResources().getString(R.string.register_verify_code));
        et_phone.getText().clear();
        et_code.getText().clear();
        cancelTimer();  // 停止倒计时
    }

    /**
     * 取消计时器
     */
    public void cancelTimer() {
        timer.cancel();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        cancelTimer();
    }

    public void setDisAllowInputPhone(boolean isAllow) {
        if (isAllow) {
            et_phone.setFocusable(true);
            et_phone.setFocusableInTouchMode(true);
            et_phone.setFilters(new InputFilter[]{
                    new InputFilter() {

                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                            return null;
                        }
                    }
            });

        } else {
            // 设置不可获取焦点
            et_phone.setFocusable(false);
            et_phone.setFocusableInTouchMode(false);
            // 输入框无法输入新的内容
            et_phone.setFilters(new InputFilter[]{
                    new InputFilter() {

                        @Override
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                            return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
                        }
                    }
            });
        }
    }
}